#!/usr/bin/env python

#    Copyright 2011,2012 Erik Persson
#
#    This file is part of the cell-sync-usrp project.
#
#    cell-sync-usrp is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    cell-sync-usrp is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with cell-sync-usrp.  If not, see <http://www.gnu.org/licenses/>.

     
a = [1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, 1]
x = [1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 1, 1]

def createSeq(a,x):
    c = []
          
    j=0
    while (j < len(x)):
        i=0
        while (i < len(a)):
            c.append(a[i]*x[j])            
            i=i+1
        j=j+1
    
    return c

def createDefSeq():
    return createSeq(a,x)

def spreadSeq(seq,sf):
    r = []
    i=0
    while (i<len(seq)):
        j=0
        while(j<sf):
            r.append(seq[i])            
            j=j+1
        i=i+1
    return r

def mulSeq(seq,val):
    r = []
    i=0
    while (i<len(seq)):
        r.append(seq[i]*val)
        i=i+1
    return r
    

if __name__ == '__main__':
    c = createDefSeq() #createSequence(a,x)

    print "SCH:",c 
    print "Length of SCH:", len(c)
    
    print "Spreading SCH with 2:"
    c2 = spreadSeq(c,2)
    print "created:",c2
    print "length",len(c2)

    print "Multiplying with (1 + 1j):"
    c3 = mulSeq(c2,(1+1j))
    print "created:",c3
    print "length",len(c3)

    print "Multiplying with (1-1j):"
    c4 = mulSeq(c2,(1-1j))
    print "created:",c4
    print "length",len(c4)

    print "Test completed"
    
    f = open("pscseq.h","w")
    f.write("gr_complex *psc = {\n")
    index = 0
    f.write("{")
    onessc = c4
    while index<len(onessc):
        if (index==len(onessc)-1):
            f.write("gr_complex(" + str(onessc[index].real) + ", " +str(onessc[index].imag) + ")")
                
        else:
            f.write("gr_complex(" + str(onessc[index].real) + ", " +str(onessc[index].imag) + ")")
            f.write(", ")
        index+=1
    f.write("}\n")
   
    
    
    cmd = raw_input("Generate test data (y/n):")
    test=1+1j
    
    if cmd=='y':
        numRep = int(raw_input("Enter number of repetitions:"))
        
        f = open("pscseq","wb")
        import struct
        
        print "Generating.....",
        rep = 0
        while (rep<numRep):
            i=0
            while (i<len(c3)):
                tmp = struct.pack("ff",c3[i].real,c3[i].imag)
                f.write(tmp)
                i=i+1
            while(i<2560*2):
                tmp = struct.pack("ff",0.0,0.0)
                f.write(tmp)
                i=i+1
            rep = rep + 1
        f.close()
        
        print "done!"
        
